home *** CD-ROM | disk | FTP | other *** search
- package sun.awt;
-
- import java.awt.AWTEvent;
- import java.util.HashSet;
- import java.util.Hashtable;
-
- public final class AWTAutoShutdown implements Runnable {
- private static final AWTAutoShutdown theInstance = new AWTAutoShutdown();
- private final Object mainLock = new Object();
- private final Object activationLock = new Object();
- private final HashSet busyThreadSet = new HashSet(7);
- private boolean toolkitThreadBusy = false;
- private final Hashtable peerMap = new PeerMap();
- private Thread blockerThread = null;
- private boolean timeoutPassed = false;
- private static final int SAFETY_TIMEOUT = 1000;
-
- private AWTAutoShutdown() {
- }
-
- public static AWTAutoShutdown getInstance() {
- return theInstance;
- }
-
- public static void notifyToolkitThreadBusy() {
- getInstance().setToolkitBusy(true);
- }
-
- public static void notifyToolkitThreadFree() {
- getInstance().setToolkitBusy(false);
- }
-
- public void notifyThreadBusy(Thread var1) {
- synchronized(this.activationLock) {
- synchronized(this.mainLock) {
- if (this.blockerThread == null) {
- this.activateBlockerThread();
- } else if (this.isReadyToShutdown()) {
- this.mainLock.notifyAll();
- this.timeoutPassed = false;
- }
-
- this.busyThreadSet.add(var1);
- }
-
- }
- }
-
- public void notifyThreadFree(Thread var1) {
- synchronized(this.activationLock) {
- synchronized(this.mainLock) {
- this.busyThreadSet.remove(var1);
- if (this.isReadyToShutdown()) {
- this.mainLock.notifyAll();
- this.timeoutPassed = false;
- }
- }
-
- }
- }
-
- void notifyPeerMapUpdated() {
- synchronized(this.activationLock) {
- synchronized(this.mainLock) {
- if (!this.isReadyToShutdown() && this.blockerThread == null) {
- this.activateBlockerThread();
- } else {
- this.mainLock.notifyAll();
- this.timeoutPassed = false;
- }
- }
-
- }
- }
-
- private boolean isReadyToShutdown() {
- return !this.toolkitThreadBusy && this.peerMap.isEmpty() && this.busyThreadSet.isEmpty();
- }
-
- private void setToolkitBusy(boolean var1) {
- if (var1 != this.toolkitThreadBusy) {
- synchronized(this.activationLock) {
- synchronized(this.mainLock) {
- if (var1 != this.toolkitThreadBusy) {
- if (var1) {
- if (this.blockerThread == null) {
- this.activateBlockerThread();
- } else if (this.isReadyToShutdown()) {
- this.mainLock.notifyAll();
- this.timeoutPassed = false;
- }
-
- this.toolkitThreadBusy = var1;
- } else {
- this.toolkitThreadBusy = var1;
- if (this.isReadyToShutdown()) {
- this.mainLock.notifyAll();
- this.timeoutPassed = false;
- }
- }
- }
- }
- }
- }
-
- }
-
- public void run() {
- Thread var1 = Thread.currentThread();
- boolean var2 = false;
- synchronized(this.mainLock) {
- try {
- this.mainLock.notifyAll();
-
- while(this.blockerThread == var1) {
- this.mainLock.wait();
- this.timeoutPassed = false;
-
- while(this.isReadyToShutdown()) {
- if (this.timeoutPassed) {
- this.timeoutPassed = false;
- this.blockerThread = null;
- break;
- }
-
- this.timeoutPassed = true;
- this.mainLock.wait(1000L);
- }
- }
- } catch (InterruptedException var10) {
- var2 = true;
- } finally {
- if (this.blockerThread == var1) {
- this.blockerThread = null;
- }
-
- }
- }
-
- if (!var2) {
- AppContext.stopEventDispatchThreads();
- }
-
- }
-
- static AWTEvent getShutdownEvent() {
- return new 1(getInstance(), 0);
- }
-
- private void activateBlockerThread() {
- Thread var1 = new Thread(this, "AWT-Shutdown");
- var1.setDaemon(false);
- this.blockerThread = var1;
- var1.start();
-
- try {
- this.mainLock.wait();
- } catch (InterruptedException var3) {
- System.err.println("AWT blocker activation interrupted:");
- var3.printStackTrace();
- }
-
- }
-
- public Hashtable getPeerMap() {
- return this.peerMap;
- }
- }
-